package dm.ast;

import test.envset.GlobalState;

/**
 * Dm的BETWEEN AND表达式
 */
public class DmBetweenExpression extends DmBoolExpression {
    DmExpression expMin = null;
    DmExpression expMax = null;

    public DmBetweenExpression() {
        this.op = new DmBoolOperator();
        this.op.setOperator("BETWEEN");
    }

    public DmExpression getMinExp() {
        return this.expMin;
    }

    public void setMinExp(DmExpression minExp) {
        this.expMin = minExp;
    }

    public DmExpression getMaxExp() {
        return this.expMax;
    }

    public void setMaxExp(DmExpression maxExp) {
        this.expMax = maxExp;
    }

    public void setExps(DmExpression exp1, DmExpression exp2, DmExpression exp3) {
        this.expLeft = exp1;
        this.expMin = exp2;
        this.expMax = exp3;
    }

    @Override
    public void setWidt() {
        if (expLeft == null || expMin == null || expMax == null) {
            wid_t = 4;
        } else {
            wid_t = 1;
            expLeft.setWidt();
            expMin.setWidt();
            expMax.setWidt();
            wid_t += expLeft.getWidt();
            wid_t += expMin.getWidt();
            wid_t += expMax.getWidt();
        }
    }

    @Override
    public void setDept() {
        if (expLeft == null || expMin == null || expMax == null) {
            dep_t = 1;
        } else {
            expLeft.setDept();
            expMin.setDept();
            expMax.setDept();
            dep_t = expLeft.getDept();
            if (expMin.getDept() > dep_t) {
                dep_t = expMin.getDept();
            }
            if (expMax.getDept() > dep_t) {
                dep_t = expMax.getDept();
            }
        }
    }

    @Override
    public String toSql() {
        if (expLeft == null || expMin == null || expMax == null) {
            return " " + this.getClass().getSimpleName() + getSqlTarget();
        } else {
            StringBuilder sql = new StringBuilder();
            sql.append(" ");
            sql.append(expLeft.toSqlClose());
            if (notFlag == true) {
                sql.append(" NOT ");
            }
            sql.append(" BETWEEN ");
            sql.append(expMin.toSqlClose());
            sql.append(" AND ");
            sql.append(expMax.toSqlClose());
            return sql.toString();
        }
    }

    @Override
    public void fill(GlobalState globalState) {
        if (!isCompleted) {
            if (expLeft == null || expMin == null || expMax == null) {
                throw new AssertionError();
            }
            expLeft.fill(globalState);
            expMin.fill(globalState);
            expMax.fill(globalState);

            //是否需要判断id between id and id 这种情形？
            //即防止between都是在同一个表的同一个列之间运算
            //todo
            isCompleted = true;
        }
    }

    //处理sql错误
    //-4097 | 单行子查询返回多行
    @Override
    public void fixErr4097(){
        fixErr4097Low(expLeft);
        fixErr4097Low(expMin);
        fixErr4097Low(expMax);
    }
}
